package com.mvp.shq.washer.jpush;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.textservice.TextInfo;

import com.google.gson.Gson;
import com.mvp.shq.washer.entity.JpushResult;
import com.mvp.shq.washer.entity.OrderInfo;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.ui.main.MainActivity;
import com.mvp.shq.washer.utils.DataDictionary;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.L;
import com.mvp.shq.washer.utils.SpUtil;
import com.orhanobut.logger.Logger;

import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2017/4/14.
 */

public class MyReceiver extends BroadcastReceiver {
    private NotificationManager nm;
    @Override
    public void onReceive(Context context, Intent intent) {
        if (nm == null){
            nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        Bundle bundle = intent.getExtras();
        L.d("onReceive - "+intent.getAction());
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())){
            L.d("JPush用户注册成功");
        }else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())){
            L.d("接受到推送下来的自定义消息");
            processCustomMessage(context,bundle);
        }else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())){
            L.d("接受到推送下来的通知");
            receivinNotification(context,bundle);
        }else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())){
            L.d("用户点击打开通知");
            openNotification(context,bundle);
        }else {
            L.d("Unhandled intent - "+intent.getAction());
        }
    }
    private void receivinNotification(Context context, Bundle bundle){
        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
        L.d("title : "+title);
        String message = bundle.getString(JPushInterface.EXTRA_ALERT);
        L.d("message ; "+message);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        Logger.d("extras : "+extras);
    }
    private void openNotification(Context context, Bundle bundle){
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        String myValue = "";
        Gson gson = new Gson();
        String type = "";
        JpushResult result = gson.fromJson(extras,JpushResult.class);
        type = result.getKey();
        Logger.d("点击打开的推送类型："+type);
        if (type == null){
            return;
        }
        if (!type.equals(DataDictionary.JPUSH_MSG) && !DataDictionary.JPUSH_COUPON.equals(type)){
            return;
        }
        Intent mIntent = new Intent(context, MainActivity.class);
        if (DataDictionary.JPUSH_MSG.equals(type)){
            bundle.putInt("jumpKey",EventCode.TO_MSG);
            RxBus.get().send(EventCode.TO_MESSAGE);
        }else if (DataDictionary.JPUSH_COUPON.equals(type)){
            bundle.putInt("jumpKey",EventCode.TO_COUPON);
            RxBus.get().send(EventCode.TO_COUPON);
        }
        mIntent.putExtras(bundle);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);

    }
    private void processCustomMessage(Context context, Bundle bundle) {
        String title = bundle.getString(JPushInterface.EXTRA_TITLE);
        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        String type = bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE);
        String content = bundle.getString(JPushInterface.EXTRA_EXTRA);
        Logger.d("收到自定义推送----type:"+type+"-------content:"+content);
        Gson gson = new Gson();
        if (type.equals(DataDictionary.JPUSH_REST)){
            //洗衣机剩余时间
            if (SpUtil.getUser() == null){
                return;
            }
            JpushResult result = gson.fromJson(content,JpushResult.class);
            int time = Integer.parseInt(result.getKey());
            OrderInfo orderInfo = SpUtil.getOrder();
            if (orderInfo != null){
                orderInfo.setRestTime(time);
                SpUtil.setOrder(orderInfo);
            }
            RxBus.get().send(EventCode.UPDATE_WASHER_TIME, time);
        }else if (DataDictionary.JPUSH_FINISH.equals(type)){
            //洗衣机结束运作
            SpUtil.setOrder(null);
            RxBus.get().send(EventCode.FINISH_WASHER);
        }else if (DataDictionary.JPUSH_MSG.equals(type)){
            //通知，添加未读消息
            int num = SpUtil.getUnReadNum();
            num ++;
            SpUtil.setUnReadNum(num);
            RxBus.get().send(EventCode.SET_UNREAD_MSG,num);
        }else if (DataDictionary.JPUSH_ERROR.equals(type)){
            SpUtil.setOrder(null);
            RxBus.get().send(EventCode.FINISH_WASHER);
        }
        //String test =  bundle.getString("test");
//        JpushResult result = gson.fromJson(content,JpushResult.class);
//        int type = result.getType();
//        RxManager rxManager = new RxManager();
//        L.d("jdy","收到自定义消息："+t ype);
//        L.d("jdy",content);
//        switch (type){
//            case 0:
//                //扫码签收成功
//                T.show("签收成功");
//                rxManager.post(Constant.SIGN_SCAN_SUCCESS,type);
//                break;
//            case 1:
//                //到付，扫描支付完成
//                T.show("支付完成");
//                rxManager.post(Constant.PAY_SUCCESS,type);
//                break;
//            case 2:
//                //新消息推送
//                rxManager.post(Constant.GET_UNREAD_NEWS_COUNT,type);
//                rxManager.post(Constant.REFRESH_NEWS,0);
//                break;
//        }
    }

}
